/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.wizards.newtask;

import java.util.Date;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.toedo.controllers.DataController;
import org.toedo.gui.dialogs.DateSelectorDialog;
import org.toedo.gui.translations.Messages;
import org.toedo.model.Task;
import org.toedo.model.validators.BaseNoteNameValidator;
import org.toedo.utils.Constants;
import org.toedo.utils.DateUtils;

public class NewTaskIntroPage extends WizardPage {
	
	private Composite mContainer;
	
	private Text mTaskTitle;
	private Text mTaskContent;
	private Spinner mTaskPriority;
	private Label mStartDateLabel;
	private Label mDueDateLabel;
	
	private Date mStartDate;
	private Date mDueDate;
	
	private BaseNoteNameValidator mTaskNameValidator;
	
	public NewTaskIntroPage() {
		super("NewTaskIntroPage"); //$NON-NLS-1$
		setTitle(Messages.NewTaskIntroPage_WizardTitle);
		setDescription(Messages.NewTaskIntroPage_IntroPageTitle);
		
		mStartDate = Task.getDefaultStartDate();
		mDueDate = Task.getDefaultDueDate();
		
		mTaskNameValidator = new BaseNoteNameValidator();
	}

	@Override
	public void createControl(final Composite parent) {
		mContainer = new Composite(parent, SWT.NULL);
		
		GridLayout gl = new GridLayout(3, false);
		mContainer.setLayout(gl);
		
		Label taskTitleLabel = new Label(mContainer, SWT.NONE);
		taskTitleLabel.setText(Messages.NewTaskIntroPage_TaskTitle);
		mTaskTitle = new Text(mContainer, SWT.BORDER);
		mTaskTitle.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false, 2, 1));
		mTaskTitle.setText(Constants.TASK_DEFAULT_NAME);
		mTaskTitle.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				setPageComplete(internalIsPageComplete());
			}			
		});
		
		mTaskContent = new Text(mContainer, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		mTaskContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
		
		Label startDate = new Label(mContainer, SWT.NONE);
		startDate.setText(Messages.NewTaskIntroPage_TaskStartDate);
		
		mStartDateLabel = new Label(mContainer, SWT.NONE);
		mStartDateLabel.setText(DateUtils.dateToDisplayString(mStartDate));
		
		Button changeStartDateButton = new Button(mContainer, SWT.PUSH);
		changeStartDateButton.setLayoutData(new GridData(SWT.RIGHT, SWT.NONE, true, false));
		changeStartDateButton.setText(Messages.NewTaskIntroPage_ModifyButton);
		changeStartDateButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DateSelectorDialog dialog =  new DateSelectorDialog(parent.getShell(), null, mStartDate);					
				int result = dialog.open();
				if (result == Dialog.OK) {
					mStartDate = dialog.getSelectedDate();
					mStartDateLabel.setText(DateUtils.dateToDisplayString(mStartDate));
				}
			}
		});
		
		Label dueDate = new Label(mContainer, SWT.NONE);
		dueDate.setText(Messages.NewTaskIntroPage_TaskDueDate);
		
		mDueDateLabel = new Label(mContainer, SWT.NONE);
		mDueDateLabel.setText(DateUtils.dateToDisplayString(mDueDate));
		
		Button changeDueDateButton = new Button(mContainer, SWT.PUSH);
		changeDueDateButton.setLayoutData(new GridData(SWT.RIGHT, SWT.NONE, true, false));
		changeDueDateButton.setText(Messages.NewTaskIntroPage_ModifyButton);
		changeDueDateButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DateSelectorDialog dialog =  new DateSelectorDialog(parent.getShell(), null, mDueDate);					
				int result = dialog.open();
				if (result == Dialog.OK) {
					mDueDate = dialog.getSelectedDate();
					mDueDateLabel.setText(DateUtils.dateToDisplayString(mDueDate));
				}
			}
		});
		
		Label taskPriorityLabel = new Label(mContainer, SWT.NONE);
		taskPriorityLabel.setText(Messages.NewTaskIntroPage_TaskPriority);
		
		mTaskPriority = new Spinner(mContainer, SWT.BORDER);
		mTaskPriority.setLayoutData(new GridData(SWT.RIGHT, SWT.NONE, true, false, 2, 1));
		mTaskPriority.setMinimum(0);
		mTaskPriority.setSelection(DataController.getInstance().getProject().getDefaultPriority());
		
		mTaskTitle.setSelection(0, mTaskTitle.getText().length());
		
		// Required to avoid an error in the system
		setControl(mContainer);
		setPageComplete(internalIsPageComplete());
	}
	
	private boolean internalIsPageComplete() {
		String result = mTaskNameValidator.isValid(mTaskTitle.getText());
		
		setErrorMessage(result);
		
		return (result == null);
	}
	
	public String getTaskTitle() {
		return mTaskTitle.getText();
	}
	
	public String getContent() {
		return mTaskContent.getText();
	}
	
	public Date getStartDate() {
		return mStartDate;
	}
	
	public Date getDueDate() {
		return mDueDate;
	}
	
	public int getTaskPriority() {
		return mTaskPriority.getSelection();
	}

}
